package com.example.common_library.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.common_library.R;
import com.example.common_library.view.shape.ShapeLinearLayout;
import com.makeramen.roundedimageview.RoundedImageView;

/**
 * @desc setting单item
 */
public class SettingItemView extends LinearLayout {
    private RelativeLayout sll_root;
    private TextView tv_left;
    private TextView tv_right;
    private RoundedImageView iv_left, iv_right;
    private String leftText, rightDefaultText;
    private int leftImage, rightImg;
    private boolean isLeftTextOrImageShow, isRightTextOrImageShow;
    private boolean isRightTextShow;
    private Context context;

    public SettingItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingItemView);
        leftImage = typedArray.getResourceId(R.styleable.SettingItemView_leftImg, 0);
        rightImg = typedArray.getResourceId(R.styleable.SettingItemView_rightImg, 0);
        leftText = typedArray.getString(R.styleable.SettingItemView_leftText);
        rightDefaultText = typedArray.getString(R.styleable.SettingItemView_defaultText);
        //左边,true,默认显示文本;false,显示图片
        isLeftTextOrImageShow = typedArray.getBoolean(R.styleable.SettingItemView_isLeftText, true);
        //右边，true,默认显示文本;false,显示图片
        isRightTextOrImageShow = typedArray.getBoolean(R.styleable.SettingItemView_isRightText, true);
        typedArray.recycle();

        initView(context);
        initData();
    }


    private void initView(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_setting_item, this, true);
        sll_root = findViewById(R.id.ll_root);
        tv_left = findViewById(R.id.tv_left);
        iv_left = findViewById(R.id.iv_left);

        iv_right = findViewById(R.id.iv_right);
        tv_right = findViewById(R.id.tv_right);
    }

    private void initData() {
        if (!TextUtils.isEmpty(leftText)) {
            tv_left.setText(leftText);
        }
        if (!TextUtils.isEmpty(rightDefaultText)) {
            tv_right.setText(rightDefaultText);
        }
        if (leftImage != 0) {
            iv_left.setBackgroundResource(leftImage);
        }
        if (leftImage != 0) {
            iv_right.setBackgroundResource(rightImg);
        }
        tv_left.setVisibility(isLeftTextOrImageShow ? VISIBLE : GONE);
        iv_left.setVisibility(isLeftTextOrImageShow ? GONE : VISIBLE);
        tv_right.setVisibility(isRightTextOrImageShow ? VISIBLE : GONE);
        iv_right.setVisibility(isRightTextOrImageShow ? GONE : VISIBLE);

        sll_root.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClick != null) {
                    itemClick.click();
                }
            }
        });

    }

    public void setDataText(String text) {
        setRightText(text, true);
    }

    public void setRightText(String text, boolean realData) {
        tv_right.setText(realData ? text : rightDefaultText);
        tv_right.setTextColor(context.getResources().getColor(
                realData ? R.color.white : R.color.color_black_A4A4A4));
    }

    public String getRightText() {
        return tv_right.getText().toString().trim();
    }

    public ImageView getRightImg() {
        return iv_right;
    }

    public interface ItemClick {
        void click();
    }

    private ItemClick itemClick;

    public void setItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }
}
